<?php
namespace app\common\validate;
use think\Validate;

class AdminUser extends Validate {
	protected $rule = [
		'roleid' => 'require',
		'fullname' => 'require',
		'abbrname' => 'require',
		'username' => 'require|unique:AdminUser',
		'register_code' => 'require',
		'oldpassword' => 'require|checkOldpassword',
		'password' => 'require|min:6|max:20',
		'repassword' => 'require|confirm:password',
		'puserid' => 'require',
		'dealerid' => 'require',
		'realname' => 'require|max:20',
		'gender' => 'require',
		'phone' => 'require|number|length:11|unique:AdminUser',
		'email' => 'email',
		'weixin' => 'require',
		'code' => 'require',
		// 'province_code' => '',
		'city_code' => 'require',
		'phonecode' => 'require|checkCode',
	];

	protected $message = [
		'fullname.require' => '经销商名称不能为空',
		'abbrname.require' => '经销商简称不能为空',
		'username.require' => '用户名不能为空',
		'username.unique' => '用户名已经存在',
		'register_code.require' => '注册码不能为空',
		'password.require' => '密码不能为空',
		'password.min' => '密码长度不能小于6位',
		'repassword.require' => '确认密码不能为空',
		'roleid.require' => '请选择用户类型',
		'repassword.confirm' => '两次输入的密码不相同',
		'oldpassword.require' => '旧密码不能为空',
		'puserid.require' => '请选择销售总监',
		'dealerid.require' => '请选择所属4S店',
		'realname.require' => '姓名/经销商简称不能为空',
		'phone.require' => '手机号不能为空',
		'realname.max' => '姓名长度不能大于10位',
		'phone.unique' => '手机号已存在',
		'phone.number' => '手机号格式不正确',
		'phone.length' => '手机号格式不正确',
		'email.require' => '邮箱不能为空',
		'email.email' => '邮箱格式不正确',
		'gender.require' => '请选择性别',
		'weixin.require' => '请输入微信号',
		'province_code.require' => '请选择省份',
		'city_code.require' => '请选择城市',
		'code.require' => '验证码不能为空',
		'code.captcha' => '验证码不正确',
		'phonecode.require' => '验证码不能为空',
	];
	protected $scene = [
		'register' => ['register_code', 'username', 'password'],
		'register_new' => ['username', 'password', 'realname', 'phone', 'phonecode', 'dealerid'],
		'change_pwd' => ['password', 'repassword', 'oldpassword'],
		'add' => ['roleid', 'username', 'password', 'realname', 'phone'],
		'add_dealer' => ['fullname', 'realname', 'username', 'password', 'phone', 'email', 'province_code', 'city_code'],
		'add_subord' => ['roleid', 'puserid', 'realname', 'gender', 'phone', 'email', 'weixin'],
		'edit' => ['realname', 'phone' => 'require|number|length:11|checkOldPhone', 'email'],
		'edit_pwd' => ['realname', 'password', 'phone' => 'require|number|length:11|checkOldPhone', 'email'],
		'edit_dealer_pwd' => ['fullname', 'abbrname', 'password', 'phone' => 'require|number|length:11|checkOldPhone', 'email', 'province_code', 'city_code'],
		'edit_dealer' => ['fullname', 'abbrname', 'phone' => 'require|number|length:11|checkOldPhone', 'email', 'province_code', 'city_code'],
		'login' => ['username|require', 'password', 'code'],
		'login_phone' => ['phone' => 'require', 'password' => 'require'],
		'edit_admin_pwd' => ['username', 'oldpassword', 'password', 'repassword'],
		'forget_pwd' => ['phone' => 'require|number|length:11|checkPhone', 'password', 'repassword', 'phonecode'],
		'check_phone_unique' => ['phone' => 'require|number|length:11|checkPhone'],
		'bind_phone_check' => ['phone', 'checkOldPhone'],
		'check_phone' => ['phone' => 'require|number|length:11'],
	];
	protected function checkPhone($value, $rule = [], $data) {
		$count = model("AdminUser")->where('phone', $value)->count();
		if ($count) {
			return true;
		} else {
			return '手机号不存在';
		}
	}
	public function checkOldPhone($value, $rule = [], $data) {
		$phone = model("AdminUser")->where('userid', $data['userid'])->value('phone');
		if ($phone != $data['phone']) {
			$count = model('AdminUser')->where('phone', $data['phone'])->count();
			if ($count) {
				return '手机号已被使用';
			}
		}
		return true;
	}
	protected function checkOldpassword($value, $rule = [], $data) {
		if (isset($data['username']) && $data['username']) {
			$where['username'] = $data['username'];
		} elseif (isset($data['phone']) && $data['phone']) {
			$where['phone'] = $data['phone'];
		} else {
			$where['userid'] = $data['userid'];
		}
		$isRight = model("AdminUser")->where($where)->where("password", md5($value))->count();
		if (!$isRight) {
			return "原密码输入错误！";
		}
		return true;
	}

	protected function checkCode($value, $rule = [], $data) {
		if (!isset($data['phonecode'])) {
			return true;
		}
		$status = check_sms($data['phone'], $data['phonecode']);
		if (!$status) {
			return '验证码输入错误';
		} else {
			return true;
		}
	}
}